package com.android.vgongyi.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.vgongyi.R;
import com.android.vgongyi.common.AppConstant;
import com.android.vgongyi.common.onMenuChangedListener;
import com.android.vgongyi.utils.SystemUtil;
import com.android.vgongyi.view.ExitAppDialog;
/**
 * Especially for controlling exit application or not and something to reuse.
 * @author Arashmen
 * */
public class BaseActivity extends Activity{
	
	private Activity actvity ;
	
    private BroadcastReceiver receiver = new BroadcastReceiver() {
		
		public void onReceive(Context context, Intent intent) {
			
			int count = intent.getIntExtra("msg_count", AppConstant.NO_0);
			TextView msgText = (TextView)actvity.findViewById(R.id.msg_count);
			if(count > AppConstant.NO_0){
				msgText.setVisibility(View.VISIBLE);
			}else{
				msgText.setVisibility(View.INVISIBLE);
			}
			msgText.setText(String.valueOf(count));
			
		}
		
	};
	
	private void registerMsgReceiver(){
    	IntentFilter filter = new IntentFilter();
		filter.addAction("MSG_COUNT_ACTION");
		registerReceiver(receiver, filter);
		
	}
	
	protected void onStop() {
		super.onStop();
		if(actvity instanceof HomeActivity ||actvity instanceof FavoriteActivity || actvity instanceof InfoActivity || actvity instanceof SettingActivity){
			unregisterReceiver(receiver);	
		}
		
	};
	
	@Override
	protected void onResume() {
		super.onResume();
		if(!SystemUtil.checkNetwork(this)){
			Toast.makeText(this, R.string.no_available_network, Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * use resource id to set the title for current activity
	 * @param activity , current activity
	 * @param resource , the title id
	 * @author Arashmen 
	 * */
	protected void setCommonTitle(Activity activity,int resource){
		TextView tv = (TextView)activity.findViewById(R.id.Common_title);
		tv.setText(resource);
	}

	/**
	 * use resource id to set the title for current activity
	 * @param activity , current activity
	 * @param title , String for title.
	 * @author Arashmen 
	 * */
	protected void setCommonTitle(Activity activity,String title){
		TextView tv = (TextView)activity.findViewById(R.id.Common_title);
		tv.setText(title);
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if (this instanceof HomeActivity
                    || this instanceof InfoActivity
                    || this instanceof FavoriteActivity
                    || this instanceof SettingActivity) {
                ExitAppDialog dialog = new ExitAppDialog(this);
                dialog.show();
            } else {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
	
	/**
	 * initialize the direction bar and set listener for menu button
	 * @author Arashmen
	 * */
	protected void MenuDirectionConfig(Activity activity){
		
		this.actvity = activity;
		
		registerMsgReceiver();
		
		setDefaultImageForDirection(activity);
		
		onMenuChangedListener menuChangedLisener = new onMenuChangedListener(activity);
		
		if(activity instanceof HomeActivity){
			((ImageView)activity.findViewById(R.id.ImageView_tab0)).setImageResource(R.drawable.tab_0_focused);
		}else if(activity instanceof FavoriteActivity) {
			((ImageView)activity.findViewById(R.id.ImageView_tab1)).setImageResource(R.drawable.tab_1_focused);
		}else if(activity instanceof InfoActivity) {
			((ImageView)activity.findViewById(R.id.ImageView_tab2)).setImageResource(R.drawable.tab_2_focused);
		}else if(activity instanceof SettingActivity) {
			((ImageView)activity.findViewById(R.id.ImageView_tab3)).setImageResource(R.drawable.tab_3_focused);
		}
		
		activity.findViewById(R.id.ImageView_tab0).setOnClickListener(menuChangedLisener);
		activity.findViewById(R.id.ImageView_tab1).setOnClickListener(menuChangedLisener);
		activity.findViewById(R.id.ImageView_tab2).setOnClickListener(menuChangedLisener);
		activity.findViewById(R.id.ImageView_tab3).setOnClickListener(menuChangedLisener);
	}
	
	/**
	 * set default image source for tabs of direction
	 * @author Arashmen 
	 * */
	private void setDefaultImageForDirection(Activity activity){
		((ImageView)activity.findViewById(R.id.ImageView_tab0)).setImageResource(R.drawable.tab_0);
		((ImageView)activity.findViewById(R.id.ImageView_tab1)).setImageResource(R.drawable.tab_1);
		((ImageView)activity.findViewById(R.id.ImageView_tab2)).setImageResource(R.drawable.tab_2);
		((ImageView)activity.findViewById(R.id.ImageView_tab3)).setImageResource(R.drawable.tab_3);
	}
	
}
